package com.bazhua.bazo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bazhua.bazo.repository.entity.Area;
import com.bazhua.bazo.repository.entity.ContextService;
import com.bazhua.bazo.repository.mapper.AreaMapper;
import com.bazhua.bazo.repository.model.AreaModel;
import com.bazhua.bazo.repository.model.AreaPageModel;
import com.bazhua.bazo.repository.model.ContextModel;
import com.bazhua.bazo.repository.model.SearchStreetModel;
import com.bazhua.bazo.repository.model.result.CommonResult;
import com.bazhua.bazo.repository.model.result.PageListResult;
import com.bazhua.bazo.repository.model.result.ResultUtils;
import com.bazhua.bazo.service.IAreaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.RecursiveTask;

@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {


    @Override
    public CommonResult GetArea() {
        return ResultUtils.ok(getAreas());
    }

    private List<Area> getAreas() {
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.eq("area_type", 1);
        List<Area> areas = baseMapper.selectList(wrapper);
        return areas;
    }

    @Override
    public PageListResult GetStreetPage(SearchStreetModel model) {
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        List<Area> areas = getAreas();

        wrapper.eq("area_type", 2);
        if (!StringUtils.isBlank(model.getStreetName()))
            wrapper.like("area_name", model.getStreetName());
        if (model.getAreaId() != null && model.getAreaId() > 0)
            wrapper.eq("parent_id", model.getAreaId());

        Page<Area> page = new Page<>(model.getPageNum(), model.getPageSize());
        page = baseMapper.selectPage(page, wrapper);
        if (page.getRecords() != null && page.getRecords().size() > 0) {
            Page<AreaPageModel> modelPage = new Page<>();
            modelPage.setCurrent(page.getCurrent());
            modelPage.setSize(page.getSize());
            modelPage.setTotal(page.getTotal());
            modelPage.setPages(page.getPages());
            modelPage.setMaxLimit(page.getMaxLimit());
            List<AreaPageModel> list = new ArrayList<>();
            for (Area area : page.getRecords()) {
                AreaPageModel areaPageModel = new AreaPageModel();
                Area parentArea = areas.stream().filter(x -> x.getId() == area.getParentId()).findFirst().orElse(new Area());
                areaPageModel.setId(area.getId());
                areaPageModel.setAreaName(area.getAreaName());
                areaPageModel.setParentAreaName(parentArea.getAreaName());
                list.add(areaPageModel);
            }
            modelPage.setRecords(list);
            return ResultUtils.pageOk(modelPage);
        }
        return ResultUtils.pageError(0, "查询数据为空");
    }

    @Override
    public CommonResult GetStreet(Long areaId) {
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.eq("area_type", 2);
        wrapper.eq("parent_id", areaId);
        List<Area> areas = baseMapper.selectList(wrapper);
        return ResultUtils.ok(areas);
    }

    @Override
    public CommonResult AddStreet(AreaModel model) {
        Area area = new Area();
        area.setAreaName(model.getAreaName());
        area.setAreaType(2);
        area.setParentId(model.getAreaId());
        area.setCreateTime(new Date());
        baseMapper.insert(area);
        return ResultUtils.ok();
    }

    @Override
    public CommonResult DeleteStreet(Long streetId) {
        Area area = baseMapper.selectById(streetId);
        if (area.getAreaType() == 2) {
            baseMapper.deleteById(streetId);
            return ResultUtils.ok();
        } else
            return ResultUtils.error(90010, "此类型不能删除");

    }
}
